import sys
sys.setrecursionlimit(1000000) #例如这里设置为十万

#导入任务（需要到resource.py里面去改,写得太挫了。。。D8Q！！）
#from define_task4 import * 

from class_task import *
from resource import resource_fresh

#各种调度方法
from Fifo_gready import *
from NDP_first_gready import *
from IO_first_gready import *
from SF_gready import *
from ALG import *

import cProfile

if __name__ == "__main__":
    print("\ttotal time,avg latency,hit,dirty")

    task_count_list = [200]
    fifo_list = []
    ndp_list = []
    io_list = []
    comb_list = []

    fifo_list_avg = []
    ndp_list_avg = []
    io_list_avg = []
    comb_list_avg = []

    fifo_list_util = []
    ndp_list_util = []
    io_list_util = []
    comb_list_util = []

    for i in task_count_list:
        if "generate_tasks" in locals().keys():
            generate_tasks(i,1,1,0,0,0)
        else:
            task_count_list = [1]

        tasks = Tasks(task_list)

        print("total",tasks.task_count,"tasks")
        # for task in tasks.task_list:
        #     print(task.task_name,task.task)

        tasks.fresh() #将任务中的状态值变为初始状态
        resource_fresh(resource) #将资源状态重置
        ret = Fifo_gready(tasks,resource)
        fifo_list.append(ret[0])
        fifo_list_avg.append(ret[1])
        fifo_list_util.append(ret[4])
        print("Fifo:",ret)

        tasks.fresh()
        resource_fresh(resource)
        ret = NDP_first_gready(tasks,resource)
        ndp_list.append(ret[0])
        ndp_list_avg.append(ret[1])
        ndp_list_util.append(ret[4])
        print("NDP First:",ret)

        tasks.fresh()
        resource_fresh(resource)
        ret = IO_first_gready(tasks,resource)
        io_list.append(ret[0])
        io_list_avg.append(ret[1])
        io_list_util.append(ret[4])
        print("IO First:",ret)

        tasks.fresh() 
        resource_fresh(resource)
        ret = alg(tasks,resource)
        #ret = cProfile.run('alg(tasks,resource)')
        comb_list.append(ret[0])
        comb_list_avg.append(ret[1])
        comb_list_util.append(ret[4])
        print("ALG:",ret)

    close_log()

    for i in range(len(task_count_list)):
        ndp_list[i] = round(ndp_list[i]/fifo_list[i],3) * 100
        io_list[i] = round(io_list[i]/fifo_list[i],3) * 100
        comb_list[i] = round(comb_list[i]/fifo_list[i],3) * 100
        fifo_list[i] = 100

        ndp_list_avg[i] = round(ndp_list_avg[i]/fifo_list_avg[i],3) * 100
        io_list_avg[i] = round(io_list_avg[i]/fifo_list_avg[i],3) * 100
        comb_list_avg[i] = round(comb_list_avg[i]/fifo_list_avg[i],3) * 100
        fifo_list_avg[i] = 100

    csv_file = open("result.csv","w")
    csv_file.write(",FCFS,I/O first,NDP first,COMB"+'\n')
    for i in range(len(task_count_list)):
        csv_file.write(str(task_count_list[i])+',')
        csv_file.write(str(fifo_list[i])+',')
        csv_file.write(str(io_list[i])+',')
        csv_file.write(str(ndp_list[i])+',')
        csv_file.write(str(comb_list[i])+',')
        csv_file.write('\n')

    #记录平均时延
    csv_file.write('\n\n')
    csv_file.write(",FCFS,I/O first,NDP first,COMB"+'\n')
    for i in range(len(task_count_list)):
        csv_file.write(str(task_count_list[i])+',')
        csv_file.write(str(fifo_list_avg[i])+',')
        csv_file.write(str(io_list_avg[i])+',')
        csv_file.write(str(ndp_list_avg[i])+',')
        csv_file.write(str(comb_list_avg[i])+',')
        csv_file.write('\n')

    #记录资源利用率
    csv_file.write('\n\n')
    csv_file.write(",FCFS,I/O first,NDP first,COMB"+'\n')
    for i in range(len(task_count_list)):
        csv_file.write(str(task_count_list[i])+',')
        csv_file.write(str(fifo_list_util[i])+',')
        csv_file.write(str(io_list_util[i])+',')
        csv_file.write(str(ndp_list_util[i])+',')
        csv_file.write(str(comb_list_util[i])+',')
        csv_file.write('\n')

    csv_file.close()

    # label_list = ['10', '50', '100', '500']    # 横坐标刻度显示值
    # x = range(len(task_count_list))

    # rects1 = plt.bar(left=x, height=fifo_list, width=0.4, alpha=0.8, color='red', label="FIFO")
    # rects2 = plt.bar(left=[i + 0.4 for i in x], height=comb_list, width=0.4, color='green', label="COMB")

    # plt.ylim(0, 180000)   # y轴取值范围
    # plt.ylabel("总执行时间")

    # plt.xticks([index + 0.2 for index in x], label_list)
    # plt.xlabel("任务量")
    # plt.title("执行时间对比")
    # plt.legend()     # 设置题注

    # plt.show()
